package com.hoperun.controller.common;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.hoperun.bean.AdminPermissions;
import com.hoperun.bean.vo.PermVO;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAdminPermissionsSV;
import com.hoperun.utils.Constant.RecordState;

/**
 * Copyright: Copyright (c) 2014 CNPCMALL <br/>
 * ClassName: BaseController.java
 * 
 * @Description: 该类的功能描述
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-8-1 上午10:51:35
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-8-1
 *        fuyu v1.0.0 修改原因
 */
public class BaseController {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(BaseController.class);

	public static final String NEWS_DETAIL_URL = "admin/news/type?category=";

	@Resource
	protected IAdminPermissionsSV permissionsSV;

	protected static List<PermVO> splitPermissions(
			List<AdminPermissions> permissions, Set<String> resource) {

		List<PermVO> vos = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(permissions)) {

			for (AdminPermissions permission : permissions) {
				/** 一级菜单 **/
				if (null == permission.getParentPermId()) {
					PermVO vo = new PermVO();
					vo.setId(permission.getPermId());
					vo.setTitle(permission.getPermName());
					vo.setUrl(permission.getUrl());
					vo.setAuthor(permission.getPermCode());
					if (CollectionUtils.isNotEmpty(resource)
							&& resource.contains(permission.getPermCode())) {
						vo.setChecked(" checked = 'checked' ");
					}
					/** 二级菜单 **/
					for (AdminPermissions permissionChild : permissions) {

						if (null == permissionChild.getParentPermId()
								|| permission.getPermId() != permissionChild
										.getParentPermId())
							continue;

						PermVO child = new PermVO();

						child.setPid(permissionChild.getParentPermId() + "");
						child.setId(permissionChild.getPermId());
						child.setTitle(permissionChild.getPermName());
						child.setUrl(permissionChild.getUrl());
						child.setAuthor(permissionChild.getPermCode());

						if (CollectionUtils.isNotEmpty(resource)
								&& resource.contains(permissionChild
										.getPermCode())) {
							child.setChecked(" checked = 'checked' ");
						}

						vo.putModule(child);

					}
					vos.add(vo);
				}
			}
		}

		return vos;
	}

	/**初始化菜单*/
	public void initMenus(final HttpServletRequest request, ModelMap modelMap)
			throws Exception {

		/**查询所有资源*/
		Criteria criteria = new Criteria();

		criteria.addEqual(AdminPermissions.COL_STATUS, RecordState.STS_USE,
				Criteria.AND);
		criteria.orderBy(AdminPermissions.COL_SORT);

		List<AdminPermissions> adminPermissions = permissionsSV
				.queryByCondition(criteria);

		modelMap.put("menusVOs", splitPermissions(adminPermissions, null));
	}

	public void outJsonString(Object dataView, HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		try {
			response.getWriter().write(JSONObject.toJSONString(dataView));
		} catch (IOException e) {
			LOGGER.debug("输出字符串时出现异常:{} ", e.toString());
			e.printStackTrace();
		}
	};
}
